<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2016/7/11 0011 10:23
 */

namespace app\modules\frontend;

use app\lib\DataMessage;
use Yii;
use yii\filters\AccessControl;
use yii\helpers\Url;

class LoginFilter extends AccessControl {
    // 重写未登录跳转，兼容ajax处理
    protected function denyAccess($user) {
        $request = Yii::$app->getRequest();
        if ($request->isAjax) {
            $urlFi = Yii::$app->request->hostInfo;
            if (strncmp($urlFi, $request->referrer, strlen($urlFi)) === 0) {
                $user->setReturnUrl($request->referrer);
            }
        } elseif ($request->isGet) {
            $user->setReturnUrl($request->url);
        }
        if($request->isAjax) {
            Yii::$app->response->content=DataMessage::initFail(302, '', $user->loginUrl);
        } else {
            Yii::$app->response->redirect($user->loginUrl);
        }
    }
}